/**
 * 用户相关接口
 */

import request from '../utils/request'
import { relogin } from '../utils/common'
import config from '../config'
import wepy from 'wepy'

/**
 *  手机密码登录
 */
async function login (params) {
  let res = await request(config.apiList.login, params, 'POST', '登录中...')
  if (res) {
    if (res.statusCode == 100) {
      wepy.showToast({
        icon: 'none',
        title: res.msg
      })
      return false
    }
    return res
  }
}

/**
 *  微信快速登录
 */
async function wxLogin (params) {
  let res = await request(config.apiList.wxLogin, params, 'POST', '登录中...')
  if (res) {
    if (res.statusCode == 100) {
      try {
        let resModal = await wepy.showModal({
          title: '提示',
          content: res.msg
        })
        if (resModal.confirm) {
          console.log('confirm')
          wepy.navigateTo({
            url: 'login'
          })
        } else if (resModal.cancel) {
          console.log('cancel')
        }
      } catch (e) {
        console.log(e)
      }
      return false
    }
    return res
  }
}

/**
 *  密码更改
 */
async function changePass (params) {
  let res = await request(config.apiList.changePass, params, 'POST', '修改中...', true)
  if (res) {
    await wepy.showToast({
      icon: 'none',
      title: res.msg
    })
    if (res.msg === '身份验证失败,请重新验证') {
      relogin()
    }
    return res
  }
}

/**
 *  客户暂存
 */
async function getguestStorage (params) {
  let res = await request(config.apiList.getguestStorage, params, 'POST', '', true)
  if (res) {
    if (res.statusCode == 100) {
      wepy.showToast({
        icon: 'none',
        title: res.msg
      })
      relogin()
      return false
    }
    return res
  }
}

/**
 *  客户卡项
 */
async function getguestProject (params) {
  let res = await request(config.apiList.getguestProject, params, 'POST', '', true)
  if (res) {
    if (res.statusCode == 100) {
      wepy.showToast({
        icon: 'none',
        title: res.msg
      })
      relogin()
      return false
    }
    return res
  }
}

/**
 *  客户消费记录
 */
async function getbuyRecords (params) {
  let res = await request(config.apiList.getbuyRecords, params, 'POST', '', true)
  if (res) {
    if (res.statusCode == 100) {
      wepy.showToast({
        icon: 'none',
        title: res.msg
      })
      relogin()
      return false
    }
    return res
  }
}

/**
 *  客户退款记录
 */
async function getrefundRecords (params) {
  let res = await request(config.apiList.getrefundRecords, params, 'POST', '', true)
  if (res) {
    if (res.statusCode == 100) {
      wepy.showToast({
        icon: 'none',
        title: res.msg
      })
      relogin()
      return false
    }
    return res
  }
}

/**
 *  客户订单详情
 */
async function getorderdetail (params) {
  let res = await request(config.apiList.getorderDetail, params, 'POST', '', true)
  if (res) {
    if (res.statusCode == 100) {
      wepy.showToast({
        icon: 'none',
        title: res.msg
      })
      relogin()
      return false
    }
    return res
  }
}

/**
 *  客户预约单
 */
async function getreservation (params) {
  let res = await request(config.apiList.getreservation, params, 'POST', '', true)
  if (res) {
    if (res.statusCode == 100) {
      wepy.showToast({
        icon: 'none',
        title: res.msg
      })
      relogin()
      return false
    }
    return res
  }
}

export {
  login,
  wxLogin,
  changePass,
  getguestStorage,
  getguestProject,
  getbuyRecords,
  getrefundRecords,
  getorderdetail,
  getreservation
}
